package logic

import (
	"context"
	"strconv"
	"time"
	"week1_zg6/orderrpc/model/mysql"

	"week1_zg6/orderrpc/internal/svc"
	"week1_zg6/orderrpc/order"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateOrderLogic) CreateOrder(in *order.CreateOrderRequest) (*order.CreateOrderResponse, error) {
	orders, err := mysql.CreateOrder(mysql.Order{
		UserId:          in.UserID,
		OrderDate:       time.Time{},
		OrderStatus:     "0",
		ShippingAddress: in.ShippingAddress,
		TotalAmount:     in.TotalAmount,
	})
	if err != nil {
		return nil, err
	}
	return &order.CreateOrderResponse{
		Data: strconv.FormatInt(orders.OrderId, 10),
	}, nil
}
